package com.neo.web;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.cache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.neo.common.result.Result;
import com.neo.config.redis.RedisUtil;
import com.neo.config.shiro.realm.MyShiroRealm;
import com.neo.entity.user.SysUser;
import com.neo.entity.user.Test;
import com.neo.service.TestMybatis;

/**
 * 任务管理
 * @author lance
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Autowired
	TestMybatis testMybatis;
	@Autowired
	RedisUtil redisUtil;
	@Autowired
	MyShiroRealm myshiroRealm;
	@RequestMapping("/login")
	public String info(ModelMap map){
		System.out.println("ssssshshshshshsh");
		Test t = new Test();
		t.setName("hah ");
		t.setDate( new Date());
		 map.put("name", "Spring Boot");
		 map.put("test", t);
		return "login";
	}
	
	@RequestMapping("/yang")
	@ResponseBody
	public Test yang(){
		System.out.println("ssssshshshshshsh");
		Test t = new Test();
		t.setName("hah ");
		t.setDate( new Date());
		
		return t;
	}
	@RequestMapping("/loginCheck")
	public String loginCheck(SysUser sysuser,HttpSession session ){
		System.out.println("开始登录！"+sysuser.getNickname()+"-------"+ sysuser.getPswd());
		UsernamePasswordToken token =null ;
		try {
			 token = new UsernamePasswordToken(sysuser.getNickname(), sysuser.getPswd(),false);
			SecurityUtils.getSubject().login(token);
			session.setAttribute("userName", sysuser.getNickname());
			System.out.println("message+++++登录成功");
			
			return "redirect:add";
		} catch (Exception e) {
			token.clear();
			
			System.out.println("fail+++++登录失败"+e);
			return "fail+++++登录失败"+e;
		}
		
	}
	
	
	@RequestMapping("/add")
	public Test add(){
		Test t = new Test();
		t.setName("hah ");
		t.setDate( new Date());
		return t;
	}
	
	@RequestMapping("/test")
	@RequiresPermissions("sss")
	public String test(){
		return "index";
	}
	
	@RequestMapping("/test1")
	public String test1(){
		return "index2";
	}
	
	@RequestMapping("/test2")
	@ResponseBody
	public String test2(){
		SecurityUtils.getSubject().logout();
		return "33333";
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String update(){
		//myshiroRealm.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
		//myshiroRealm.clearAllCachedAuthorizationInfo();
		//myshiroRealm.clearAllCache();
		
	
		redisUtil.remove("getAll");
		return "222";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public Result getAll(){
		return testMybatis.getAll();
	}
	
/*
    @RequestMapping("/outPutOrderItem")
    public void outPutOrderItem(@RequestParam(name = "orderCode", required = false) String orderCode,
                                @RequestParam(name = "customerNick", required = false) String customerNick,
                                @RequestParam(name = "createTimeStart", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTimeStart,
                                @RequestParam(name = "createTimeEnd", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTimeEnd,
                                @RequestParam(name = "status", required = false) String status,
                                @RequestParam(name = "payTimeStart", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date payTimeStart,
                                @RequestParam(name = "payTimeEnd", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date payTimeEnd, HttpServletRequest request, HttpServletResponse response) {

        OrderCriteria criteria = new OrderCriteria();
        User currentUser = userService.getCurrentUser();
//        criteria.setOrgId(currentUser.getOrgId());
        criteria.setPageSize(500000);
        criteria.setOrderCode(orderCode);
        criteria.setCustomerNick(customerNick);
        criteria.setCreateTimeStart(createTimeStart);
        criteria.setCreateTimeEnd(createTimeEnd);
        criteria.setStatus(status);
        criteria.setPayTimeStart(payTimeStart);
        criteria.setPayTimeEnd(payTimeEnd);

        List<OutPutOrderItemDTO> list = portalItemService.outPutOrderItem(criteria);
        String title = "商家订单明细";
        String[] hearders = new String[]{
                "主订单号", "订单号", "商家名称", "下单时间", "支付时间", "完成时间", "订单状态", "订单金额",
                "物流公司", "物流单号", "昵称", "电话号码",
                "商品名称", "商品类型", "商品价格", "商品数量"

        };//表头数组
        String[] fields = new String[]{
                "mainOrderCode", "orderCode", "orgName", "createTime", "payTime", "confirmedTime", "statusName", "amount",
                "logisticsCompany", "logisticsTrackingNo", "customerNick", "customerTel",
                "itemName", "itemTypeName", "itemPrice", "itemCount"
        };
        String creator = currentUser.getRealName();
        try {
            outPutService.outPutExcel(list, title, hearders, fields, creator, request, response);
        } catch (Exception e) {
            e.printStackTrace();

        }

    }*/
}
